package com.miyi.core;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;

import com.miyi.utils.Tools;



public class PValidateConfig {

	public  Map<String,List<PValidate>> map;
	protected  Predicate<Object> required = Tools::isNotEmpty;
	protected  Predicate<Object> isNumeric = Tools::isNumeric;
	
	
	public PValidateConfig(){
		
		map = new HashMap<String, List<PValidate>>();
		
		List<PValidate> vCustomer = new ArrayList<PValidate>();
		vCustomer.add(new PValidate("name","姓名不能为空",required));
		vCustomer.add(new PValidate("name","姓名长度不能大于4",(s)->String.valueOf(s).length()<=4));
		vCustomer.add(new PValidate("age","年龄不能为空",required));
		vCustomer.add(new PValidate("age","年龄请输入数字",isNumeric));
		vCustomer.add(new PValidate("age","年龄不能多于两位",(s)->Tools.isRegexMatch(s, "^[0-9]{1,2}$")));
		map.put("customer", vCustomer);
		/*
		List<PValidate> vUser = new ArrayList<PValidate>();
		vUser.add(new PValidate("username","名字不能为空",required));
		vUser.add(new PValidate("password","密码不能为空",required));
		vUser.add(new PValidate("vcode","验证码不能为空",required));
		
		vUser.add(new PValidate("username","名字输入不符合规范",(s)->Tools.isRegexMatch(s, "^[a-zA-Z_][a-zA-Z0-9_]{6,10}")));
		vUser.add(new PValidate("password","密码太简单不符合规范",(s)->Tools.isRegexMatch(s, "^[0-9a-zA-Z]{6,16}$")));
		vUser.add(new PValidate("email","邮箱格式不匹配",(s)->Tools.isRegexMatch(s, "^\\w+@\\w+\\.(\\w{2,3}|\\w{2,3}\\.\\w{2,3})$")));
		vUser.add(new PValidate("mobile","手机号输入错误",(s)->Tools.isRegexMatch(s, "^1(3|4|5|7|8)\\d{9}$")));
		vUser.add(new PValidate("age","年龄输入错误",(s)->Tools.isRegexMatch(s, "^[0-9]{1,2}$")));
		vUser.add(new PValidate("postcode","邮政编码输入错误",(s)->Tools.isRegexMatch(s, "^\\d{6}$")));
		vUser.add(new PValidate("IDCode","身份证号输入错误",(s)->Tools.isRegexMatch(s, "^(\\d{15}|\\d{18}|\\d{17}X)$")));
		map.put("usertest", vUser);
		
		List<PValidate> user = new ArrayList<PValidate>();
		user.add(new PValidate("name","名字不能为空",required));
		//user.add(new PValidate("age","年龄不能为空",required));
		//user.add(new PValidate("age","年龄请输入数字",isNumeric));
		map.put("user", user);
		*/
		List<PValidate> liuyb = new ArrayList<PValidate>();
		liuyb.add(new PValidate("name","名字不能为空",required));
		map.put("liuyb", liuyb);
		
		List<PValidate> message = new ArrayList<PValidate>();
		message.add(new PValidate("name","名字不能为空",required));
		map.put("message", message);
		
	}
	
    private static class SingletonHolder {  
    	private static final PValidateConfig INSTANCE = new PValidateConfig();  
    	
    }  
    public static final PValidateConfig getInstance() {  
        return SingletonHolder.INSTANCE;  
    }  
    
	public  List<PValidate> get(String name){
		return map.get(name);
	}
	public  void set(String name, List<PValidate> vmap){
		if(map.containsKey(name)){
			map.replace(name, vmap);
		}else{
			map.put(name, vmap);
		}
	}
	
	
	
}
